package cn.edu.njust.listener;

import cn.edu.njust.service.impl.MyTask;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;

import java.util.Timer;


//@WebListener
public class ContextListener implements ServletContextListener {
    private Timer timer = null;

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        timer = new Timer(true);
        servletContextEvent.getServletContext().log("定时器已启动");
        timer.schedule(new MyTask(servletContextEvent.getServletContext()),0,60*60*1000);
        servletContextEvent.getServletContext().log("添加任务");
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        timer.cancel();
        servletContextEvent.getServletContext().log("销毁定时器");
    }
}
